package packageController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import packageBusiness.ProduitManager;
import packageException.*;
import packageModel.Syllabus;

/**
 *
 * @author Lionel & Julien
 */
public class ProduitSyllatheque extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            
            // ---------------------------
            // Création du syllabus aléatoire
            Syllabus syllabus = null;
            ProduitManager syl = new ProduitManager();

            // Syllabus aléatoire
            ProduitManager nbSyll = new ProduitManager();
            Random r = new Random();
            System.out.println("Coucou");
            int valeur = 1 + r.nextInt(nbSyll.nbSyllabus());
            
            // On attend qu'on ait récupéré l'id d'un syllabus séléctionné 
            // aléatoirement par une méthode Java (Random)
            syllabus = syl.SyllabusHasard(valeur);
            System.out.println("Coucou");
            
            // ---------------------------
            // Liste des 6 derniers syllabus
            // On affichera ces syllabus dans la page d'accueil
            System.out.println("------Coucou");
            ArrayList<Syllabus> listSyllabus = new ArrayList<Syllabus>();
            ProduitManager listSyl = new ProduitManager();
            
            // On créé notre liste
            listSyllabus = listSyl.getSyllabus();
            System.out.println("Coucou");
            
            // ---------------------------
            // Création des variables pour la vue
            request.setAttribute("syllabus", syllabus);
            request.setAttribute("listSyllabus", listSyllabus);
        } catch(SyllabusException e1) {
            // Erreur
            request.setAttribute("error", e1.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        } catch(LangueException e2) {
            // Erreur
            request.setAttribute("error", e2.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        } catch(CategorieException e3) {
            // Erreur
            request.setAttribute("error", e3.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        } catch(AuteurException e4) {
            // Erreur
            request.setAttribute("error", e4.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        } catch(InvalideDonneesException e5) {
            // Erreur
            request.setAttribute("errorInvalideDonnees", e5.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        } catch(DataException e6) {
            // Erreur
            request.setAttribute("error", e6.toString());
            RequestDispatcher rd = request.getRequestDispatcher("erreur.jsp");
            rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
